from django.core.paginator import Paginator
from django.http import JsonResponse
from django.shortcuts import render, redirect
from .models import *
from django.db.models import Q
from datetime import datetime, timedelta
import os
import csv


# 定义登录验证装饰器
def checkLogin(func):
    def check(request, *args):
        username = request.session.get("userName")
        if username:
            return func(request, *args)
        else:
            return redirect("/manager/login/")
    return check


@checkLogin
def index(request):
    return render(request, "manager/index.html")


def login(request):
    """
    用户登录视图函数
    :param request:
    :return:
    """
    if request.method == "GET":
        return render(request, "manager/login.html")
    elif request.method == "POST":
        # 从前端页面获取用户输入的用户名和密码
        username = request.POST.get("userName")
        password = request.POST.get("password")
        users = User.objects.filter((Q(userName=username) & Q(password=password)) |
                                    (Q(phone=username) & Q(password=password)) |
                                    (Q(email=username) & Q(password=password)))
        if users:
            # 把用户信息存储到session中，可以在前端等需要的地方进行获取显示
            request.session['userName'] = users[0].userName
            request.session['id'] = users[0].id
            request.session['password'] = password
            return render(request, "manager/index.html")
        else:
            print("用户名或密码错误！")
        return render(request, "manager/login.html")


@checkLogin
def userinfo(request):
    """
    获取用户的基本信息
    :param request:
    :return:
    """
    id = request.session.get("id")
    if request.method == 'GET':
        if id:
            user = User.objects.get(pk=id)
        return render(request, "manager/userinfo.html", {"user": user})
    elif request.method == 'POST':
        # 修改用户的基本信息
        userName = request.POST.get("userName")
        phone = request.POST.get("phone")
        company = request.POST.get("company")
        depart = request.POST.get("depart")
        email = request.POST.get("email")
        remark = request.POST.get("remark")
        user = User.objects.get(pk=id)
        user.userName = userName
        user.phone = phone
        user.company = company
        user.email = email
        user.depart = depart
        user.remark = remark
        user.save()
        return render(request, "manager/userinfo.html", {"user": user})


@checkLogin
def modifyPassword(request):
    if request.method == 'GET':
        return render(request, "manager/modifyPassword.html")
    elif request.method == 'POST':
        # 修改密码
        oldpassword = request.POST.get("oldPassword")
        newPassword = request.POST.get("newPassword")
        confirmPassword = request.POST.get("confirmPassword")
        # 获取当前用户
        id = request.session.get("id")
        user = User.objects.get(pk=id)
        if oldpassword != user.password:
            print("旧密码错误！")
            msg = "旧密码错误！"
            return render(request, "manager/modifyPassword.html", {"msg": msg})
        else:
            if newPassword != confirmPassword:
                print("新密码和确认密码不一致！")
                msg = "新密码和确认密码不一致！"
                return render(request, "manager/modifyPassword.html", {"msg": msg})
            else:
                user.password = newPassword
                user.save()
                msg = "密码修改成功！"
        return render(request, "manager/modifyPassword.html", {"msg": msg})


def logout(request):
    """
    退出系统登录：1.清除登录时候设置的session信息 2.跳转到登录页面
    :param request:
    :return:
    """
    # del request.session['userName']
    # del request.session['id']
    request.session.clear()
    return render(request, "manager/login.html")


def addSpiderTask(request):
    """
    数据采集任务添加功能实现
    :param requests:
    :return:
    """
    if request.method == 'GET':
        return render(request, "manager/addSpiderTask.html")
    elif request.method == 'POST':
        # 获取用户输入的数据
        taskName = request.POST.get("taskName")
        taskUrl = request.POST.get("taskUrl")
        createTime = datetime.now()
        createBy = request.session["id"]
        startTime = request.POST.get("startTime")
        endTime = request.POST.get("endTime")
        status = request.POST.get("status")
        type = request.POST.get("type")
        remark = request.POST.get("remark")

        task = SpiderTask()
        task.taskName = taskName
        task.taskUrl = taskUrl
        task.createTime = createTime
        task.createBy = createBy
        task.startTime = startTime
        task.endTime = endTime
        task.status = status
        task.type = type
        task.remark = remark
        task.save()
        return redirect("/manager/spiderList/")


def spiderList(request):
    """
    查询所有数据
    :param request:
    :return:
    """
    if request.method == 'GET':
        spiderList = SpiderTask.objects.all()
        pageSize = 2
        paginator = Paginator(spiderList, pageSize)
        # 获取当前页
        page = request.GET.get("page")
        if page == None or page == "":
            page = 1
        # 获取当前页对应的数据
        objects = paginator.page(page)
        # 获取总记录数据
        total = paginator.count
        # 获取总页数
        totalPage = paginator.num_pages
        return render(request, "manager/spiderList.html", locals())

    else:
        # 从页面获取用户提交的条件数据，根据条件查询数据表
        taskName = request.POST.get("taskName")
        type = request.POST.get("type")
        status = request.POST.get("status")
        if taskName != "" and type != "" and status != "":
            # select * from tb_spider where taskName like % taskName  模糊查询
            datas = SpiderTask.objects.filter(Q(taskName__contains=taskName) & Q(type=type) & Q(status=status))
        elif taskName != "" and type != "" and status == "":
            datas = SpiderTask.objects.filter(Q(taskName__contains=taskName) & Q(type=type))
        elif taskName != "" and type == "" and status != "":
            datas = SpiderTask.objects.filter(Q(taskName__contains=taskName) & Q(status=status))
        elif taskName != "" and type == "" and status == "":
            datas = SpiderTask.objects.filter(taskName__contains=taskName)
        elif taskName == "" and type != "" and status != "":
            datas = SpiderTask.objects.filter(Q(type=type) & Q(status=status))
        elif taskName == "" and type == "" and status != "":
            datas = SpiderTask.objects.filter(status=status)
        elif taskName == "" and type != "" and status == "":
            datas = SpiderTask.objects.filter(type=type)
        else:
            datas = SpiderTask.objects.all()

        pageSize = 2
        paginator = Paginator(datas, 2)
        page = request.POST.get("page")
        if page == None or page == "":
            page = 1
        objects = paginator.page(page)
        total = paginator.count
        total_pages = paginator.num_pages

        return render(request, "manager/spiderList.html", locals())


def updateSpiderTask(request, id):

    # 根据id查询任务数据对象
    if id:
        task = SpiderTask.objects.get(pk=id)
    if request.method == 'GET':
        return render(request, "manager/updateSpiderTask.html", locals())
    else:
        # 获取数据，重新赋值，进行修改
        # 获取用户输入的数据
        taskName = request.POST.get("taskName")
        taskUrl = request.POST.get("taskUrl")
        executeTime = request.POST.get("executeTime")
        executeBy = request.POST.get("executeBy")
        startTime = request.POST.get("startTime")
        endTime = request.POST.get("endTime")
        status = request.POST.get("status")
        type = request.POST.get("type")
        remark = request.POST.get("remark")

        task.taskName = taskName
        task.taskUrl = taskUrl
        task.executeTime = executeTime
        task.executeBy = executeBy
        task.startTime = startTime
        task.endTime = endTime
        task.status = status
        task.type = type
        task.remark = remark
        task.save()
        return redirect("/manager/spiderList/")


def deleteSpider(request, id):
    if id:
        task = SpiderTask.objects.get(pk=id)
        if task:
            task.delete()
            msg = "删除成功！"
        else:
            msg = "没有查询到相关记录"
    else:
        msg = "id为空！"
    return redirect("/manager/spiderList/", locals())


def uploadAnalysisFile(request):
    if request.method == "GET":
        return render(request, "manager/uploadAnalysisFile.html")
    elif request.method == 'POST':
        # 实现文件上传，保存数据库表，页面跳转
        fileName = request.POST.get("fileName")
        fileUrl = request.FILES.get("fileUrl")
        fileSize = fileUrl.size
        uploadTime = datetime.now()
        uploadBy = request.session['id']
        fileType = request.POST.get("fileType")
        fileStatus = request.POST.get("fileStatus")
        remark = request.POST.get("remark")
        analysis = Analysis()
        analysis.fileName = fileName
        analysis.fileUrl = fileUrl
        analysis.fileSize = fileSize
        analysis.uploadTime = uploadTime
        analysis.uploadBy = uploadBy
        analysis.fileType = fileType
        analysis.fileStatus = fileStatus
        analysis.remark = remark
        analysis.save()
        return redirect("/manager/analysisFileList/")


def analysisFileList(request):
    if request.method == 'GET':
        # 查询所有数据
        analysisList = Analysis.objects.all()
        # 分页显示
        pageSize = 2
        paginator = Paginator(analysisList, pageSize)
        # 获取当前页
        page = request.GET.get("page")
        if page == None or page == "":
            page = 1
        # 获取当前页对应的数据
        objects = paginator.page(page)
        # 获取所有记录条数
        total = paginator.count
        return render(request, "manager/analysisFileList.html", locals())
    elif request.method == 'POST':
        # 多条件查询
        fileName = request.POST.get("fileName")
        fileType = request.POST.get("fileType")
        fileStatus = request.POST.get("fileStatus")
        if fileName != "" and fileType != "" and fileStatus != "":
            analysisList = Analysis.objects.filter(Q(fileName__contains=fileName) & Q(fileType=fileType) & Q(fileStatus=fileStatus))
        elif fileName != "" and fileType != "" and fileStatus == "":
            analysisList = Analysis.objects.filter(Q(fileName__contains=fileName) & Q(fileType=fileType))
        elif fileName != "" and fileType == "" and fileStatus != "":
            analysisList = Analysis.objects.filter(Q(fileName__contains=fileName)& Q(fileStatus=fileStatus))
        elif fileName != "" and fileType == "" and fileStatus == "":
            analysisList = Analysis.objects.filter(fileName__contains=fileName)
        elif fileName == "" and fileType != "" and fileStatus != "":
            analysisList = Analysis.objects.filter(Q(fileType=fileType) & Q(fileStatus=fileStatus))
        elif fileName == "" and fileType == "" and fileStatus != "":
            analysisList = Analysis.objects.filter(fileStatus=fileStatus)
        elif fileName == "" and fileType != "" and fileStatus == "":
            analysisList = Analysis.objects.filter(fileType=fileType)
        else:
            analysisList = Analysis.objects.all()
        # 查询结果分页
        pageSize = 2
        paginator = Paginator(analysisList, pageSize)
        # 获取当前页
        page = request.POST.get("page")
        if page == None or page == "":
            page = 1
        # 获取当前页对应的数据
        objects = paginator.page(page)
        # 总条数
        total = paginator.count
        return render(request, "manager/analysisFileList.html", locals())


def updateAnalysisFile(request, id):
    if request.method == 'GET':
        # 根据id进行数据查询
        if id:
            analysis = Analysis.objects.get(pk=id)
            if analysis:
                return render(request, "manager/updateAnalysisFile.html", locals())
    elif request.method == 'POST':
        # 获取页面数据，进行修改操作
        fileName = request.POST.get("fileName")
        fileUrl = request.FILES.get("fileUrl")

        executeTime = request.POST.get("executeTime")
        executeBy = request.POST.get("executeBy")
        fileType = request.POST.get("fileType")
        fileStatus = request.POST.get("fileStatus")
        remark = request.POST.get("remark")

        analysis = Analysis.objects.get(pk=id)
        analysis.fileName = fileName
        if fileUrl:
            # 删除原来的文件，保存新的文件
            url = "./media/" + str(analysis.fileUrl)
            if os.path.exists(url):
                os.remove(url)
                print("文件删除成功！")
            else:
                print("文件不存在！")
            analysis.fileUrl = fileUrl
            analysis.fileSize = fileUrl.size
        analysis.executeTime = executeTime
        analysis.executeBy = executeBy
        analysis.fileType = fileType
        analysis.fileStatus = fileStatus
        analysis.remark = remark
        analysis.save()
        return redirect("/manager/analysisFileList/")


def deleteAnalysisFile(request, id):
    if id:
        analysis = Analysis.objects.get(pk=id)
        if analysis:
            # 从数据库表中删除记录
            analysis.delete()
            # 删除文件服务器上面的文件
            path = './media/' + str(analysis.fileUrl)
            if os.path.exists(path):
                os.remove(path)
                print("文件删除成功！")
            else:
                print("删除的文件不存在！")
    return redirect("/manager/analysisFileList/")


def addMember(request):
    """
    添加会员信息：1.get: 跳转到会员信息添加页面 2.post:保存会员信息到数据库表中
    :param request:
    :return:
    """
    if request.method == 'GET':
        return render(request, "manager/addMember.html")
    elif request.method == 'POST':
        member = Member()
        member.account = request.POST.get("account")
        member.password = request.POST.get("password")
        member.gender = request.POST.get("gender")
        member.birthday = request.POST.get("birthday")
        member.realName = request.POST.get("realName")
        member.phone = request.POST.get("phone")
        member.email = request.POST.get("email")
        member.province = request.POST.get("province")
        member.city = request.POST.get("city")
        member.address = request.POST.get("address")
        member.type = request.POST.get("type")
        member.status = request.POST.get("status")
        member.shopId = request.POST.get("shopId")
        member.points = request.POST.get("points")
        member.balance = request.POST.get("balance")
        member.registerTime = datetime.now()
        member.registerBy = request.session['id']
        # member.remark = request.POST.get("remark")
        headUrl = request.FILES.get("headUrl")
        if headUrl:
            member.headUrl = headUrl
        member.save()
        return redirect("/manager/memberList/")


def memberList(request):
    if request.method == 'GET':
        memberList = Member.objects.all()
        # get提交获取当前页
        page = request.GET.get("page")
        if page == None or page == "":
            page = 1
    elif request.method == 'POST':
        # 多条件查询
        account = request.POST.get("account")
        phone = request.POST.get("phone")
        type = request.POST.get("type")
        if account != "" and phone != "" and type != "":
            memberList = Member.objects.filter(Q(account=account) & Q(phone=phone) & Q(type=type))
        elif account != "" and phone != "" and type == "":
            memberList = Member.objects.filter(Q(account=account) & Q(phone=phone))
        elif account != "" and phone == "" and type != "":
            memberList = Member.objects.filter(Q(account=account) & Q(type=type))
        elif account != "" and phone == "" and type == "":
            memberList = Member.objects.filter(account=account)
        elif account == "" and phone != "" and type != "":
            memberList = Member.objects.filter(Q(phone=phone) & Q(type=type))
        elif account == "" and phone == "" and type != "":
            memberList = Member.objects.filter(type=type)
        elif account == "" and phone != "" and type == "":
            memberList = Member.objects.filter(phone=phone)
        else:
            memberList = Member.objects.all()

        # post方式获取page当前页
        page = request.POST.get("page")
        if page == None or page == "":
            page = 1

    pageSize = 10
    paginator = Paginator(memberList, pageSize)
    objects = paginator.page(page)
    total = paginator.count

    return render(request, "manager/memberList.html", locals())


def updateMember(request, id):
    if id:
        member = Member.objects.get(pk=id)
    if request.method == 'GET':
        return render(request, "manager/updateMember.html", locals())
    elif request.method == 'POST':
        # 数据获取和存储
        member.account = request.POST.get("account")
        member.password = request.POST.get("password")
        member.gender = request.POST.get("gender")
        member.birthday = request.POST.get("birthday")
        member.realName = request.POST.get("realName")
        member.phone = request.POST.get("phone")
        member.email = request.POST.get("email")
        member.province = request.POST.get("province")
        member.city = request.POST.get("city")
        member.address = request.POST.get("address")
        member.type = request.POST.get("type")
        member.status = request.POST.get("status")
        member.shopId = request.POST.get("shopId")
        member.points = request.POST.get("points")
        member.balance = request.POST.get("balance")
        headUrl = request.FILES.get("headUrl")
        if headUrl:
            # 删除原来的旧头像
            path = "./media/" + str(member.headUrl)
            if os.path.exists(path):
                os.remove(path)
                print("头像删除成功！")
            else:
                print("头像不存在！")
            member.headUrl = headUrl
        member.save()
        return redirect("/manager/memberList/")


def deleteMember(request, id):
    """
    ajax请求：
    :param request:
    :param id:
    :return: json
    """
    if id:
        member = Member.objects.get(pk=id)
        if member:
            member.delete()
            msg = "success"
        else:
            msg = "没有找到该对象！"
    else:
        msg = "id的值为空！"
    return JsonResponse({"msg": msg})


def importMember(request):
    """
    数据导入视图：1. 跳转到导入页面 2. 读取文件，遍历文件数据存储到数据库表中
    :param request:
    :return:
    """
    if request.method == "GET":
        return render(request, "manager/importMember.html")
    elif request.method == "POST":
        # 读取并解析文件中的数据，存入到数据库表中
        file = request.FILES.get("fileUrl")
        datas = file.read().decode("utf-8").splitlines()
        results = csv.DictReader(datas)
        for row in results:
            member = Member()
            member.account = row.get('account')
            member.password = row.get('password')
            member.realName = row.get('realName')
            member.gender = row.get('gender')
            member.phone = row.get('phone')
            # member.birthday = row.get('birthday')
            # 日期类型数据的格式转换
            data_format = datetime.strptime(row.get('birthday'), '%d/%m/%Y %H:%M:%S')
            member.birthday = data_format.strftime('%Y-%m-%d %H:%M:%S')
            member.email = row.get('email')
            member.headUrl = row.get('headUrl')
            member.points = row.get('points')
            member.balance = row.get('balance')
            member.address = row.get('address')
            # member.registerTime = row.get('registerTime')
            # 日期类型数据的格式转换
            dt_format = datetime.strptime(row.get('registerTime'), '%d/%m/%Y %H:%M:%S')
            member.registerTime = dt_format.strftime('%Y-%m-%d %H:%M:%S')
            member.status = row.get('status')
            member.type = row.get('type')
            member.remark = row.get('remark')
            member.city = row.get('city')
            member.province = row.get('province')
            member.shopId = row.get('shopId')
            # 存储数据
            member.save()
        return redirect("/manager/memberList/")


def addOrder(request):
    """
    添加订单信息
    :param request:
    :return:
    """
    if request.method == 'GET':
        return render(request, "manager/addOrder.html")
    elif request.method == 'POST':
        # 获取数据，存储到数据库
        order = Order()
        account = request.POST.get("account")

        member = Member.objects.filter(account=account)
        if member:
            member = member[0]
        order.member = member

        order.buyTime = request.POST.get("buyTime")
        order.payType = request.POST.get("payType")
        order.payMoney = request.POST.get("payMoney")
        order.buyShop = Shop.objects.get(pk=request.POST.get("buyShop"))
        order.type = request.POST.get("type")
        order.status = request.POST.get("status")
        order.remark = request.POST.get("remark")
        order.save()
        # 跳转到订单明细页面，继续添加购买的商品信息
        return render(request, "manager/addOrderDetail.html", locals())


def addOrderDetail(request):
    if request.method == 'GET':
        return render(request, "manager/addOrderDetail.html", locals())
    if request.method == 'POST':
        od = OrderDetail()
        orderId = request.POST.get("orderId")
        order = Order.objects.get(pk=orderId)
        od.order = order
        od.productName = request.POST.get("productName")
        od.salePrice = request.POST.get("salePrice")
        od.discountPrice = request.POST.get("discountPrice")
        od.numbers = request.POST.get("numbers")
        od.variant = request.POST.get("variant")
        od.industry = request.POST.get("industry")
        shop = Shop.objects.get(pk=request.POST.get("shopId"))
        od.shop = shop
        od.totalMoney = request.POST.get("totalMoney")
        od.remark = request.POST.get("remark")
        od.save()

        return render(request, "manager/addOrderDetail.html", locals())


def orderList(request):
    if request.method == 'GET':
        orderList = Order.objects.all()
        page_size = 10
        paginator = Paginator(orderList, page_size)
        # 获取当前页
        page = request.GET.get("page")
        if page is None or page == "":
            page = 1
        # 获取当前页对应的数据
        objects = paginator.page(page)
        # 获取总记录数据
        total = paginator. count
    elif request.method == 'POST':
        account = request.POST.get("memberId")
        type = request.POST.get("type")
        status = request.POST.get("status")
        if account != "" and type != "" and status != "":
            member = Member.objects.filter(account=account)
            if member:
                member = member[0]
            orderList = Order.objects.filter(Q(member=member) & Q(type=type) & Q(status=status))
        elif account != "" and type != "" and status == "":
            member = Member.objects.filter(account=account)
            if member:
                member = member[0]
            orderList = Order.objects.filter(Q(member=member) & Q(type=type))
        elif account != "" and type == "" and status != "":
            member = Member.objects.filter(account=account)
            if member:
                member = member[0]
            orderList = Order.objects.filter(Q(member=member) & Q(status=status))
        elif account != "" and type == "" and status == "":
            member = Member.objects.filter(account=account)
            if member:
                member = member[0]
            orderList = Order.objects.filter(member=member)
        elif account == "" and type != "" and status != "":
            orderList = Order.objects.filter(Q(type=type) & Q(status=status))
        elif account == "" and type == "" and status != "":
            orderList = Order.objects.filter(status=status)
        elif account == "" and type != "" and status == "":
            orderList = Order.objects.filter(type=type)
        else:
            orderList = Order.objects.all()
        page_size = 10
        paginator = Paginator(orderList, page_size)
        # 获取当前页
        page = request.POST.get("page")
        if page is None or page == "":
            page = 1
        # 获取当前页对应的数据
        objects = paginator.page(page)
        # 获取总记录数据
        total = paginator.count

    return render(request, "manager/orderList.html", locals())


def orderDetailList(request):
    """
    订单明细列表：根据订单ID编号查询
    :param request:
    :return:
    """
    if request.method == 'GET':
        orderId = request.GET.get("orderId")
        if orderId:
            order = Order.objects.get(pk=orderId)
            detailList = OrderDetail.objects.filter(order=order)
        else:
            detailList = OrderDetail.objects.all()
    elif request.method == 'POST':
        productName = request.POST.get("productName")
        detailId = request.POST.get("detailId")
        industry = request.POST.get("industry")

        if productName != "" and detailId != "" and industry != "":
            detailList = OrderDetail.objects.filter(Q(productName__contains=productName) & Q(id=detailId) & Q(industry=industry))
        elif productName != "" and detailId != "" and industry == "":
            detailList = OrderDetail.objects.filter(Q(productName__contains=productName) & Q(id=detailId))
        elif productName != "" and detailId == "" and industry != "":
            detailList = OrderDetail.objects.filter(Q(productName__contains=productName) & Q(industry=industry))
        elif productName != "" and detailId == "" and industry == "":
            detailList = OrderDetail.objects.filter(productName__contains=productName)
        elif productName == "" and detailId != "" and industry != "":
            detailList = OrderDetail.objects.filter(Q(id=detailId) & Q(industry=industry))
        elif productName == "" and detailId == "" and industry != "":
            detailList = OrderDetail.objects.filter(industry=industry)
        elif productName == "" and detailId != "" and industry == "":
            detailList = OrderDetail.objects.filter(Q(id=detailId))
        else:
            detailList = OrderDetail.objects.all()

    page_size = 10
    paginator = Paginator(detailList, page_size)
    page = request.GET.get("page")
    if page is None or page == "":
        page = 1
    objects = paginator.page(page)
    total = paginator.count

    return render(request, "manager/orderDetailList.html", locals())


def updateOrder(request, id):
    if id:
        order = Order.objects.get(pk=id)
    if request.method == 'GET':
        # 放置页面数据
        return render(request, "manager/updateOrder.html", locals())
    elif request.method == 'POST':
        account = request.POST.get("account")
        member = Member.objects.filter(account=account)
        if member:
            member = member[0]
        order.member = member

        order.buyTime = request.POST.get("buyTime")
        order.payType = request.POST.get("payType")
        order.payMoney = request.POST.get("payMoney")
        order.buyShop = Shop.objects.get(pk=request.POST.get("buyShop"))
        order.type = request.POST.get("type")
        order.status = request.POST.get("status")
        order.updateBy = request.POST.get("updateBy")
        order.updateTime = request.POST.get("updateTime")
        order.remark = request.POST.get("remark")
        order.save()
        # 获取页面数据
        return redirect("/manager/orderList/")


def deleteOrder(request, id):
    """
    http://127.0.0.1:8000/manager/deleteOrder/1
    订单删除功能实现：使用ajax异步请求实现， 删除主表数据之前要先删除子表关联的数据
    :param request:
    :param id:
    :return: json
    """
    if id:
        # 根据id查询订单对象
        order = Order.objects.get(pk=id)
        if order:
            # 根据order对象查询订单明细列表
            detailList = OrderDetail.objects.filter(order=order)
            for detail in detailList:
                # 删除子表数据
                detail.delete()
            # 删除订单数据
            order.delete()
            msg = "success"
        else:
            msg = "没有找到删除对象！"
    else:
        msg = "ID不能为空！"
    return JsonResponse({"msg": msg})


def getMemberDatas(request):
    """
    获取会员数据接口：
    http://127.0.0.1:8000/manager/getMemberDatas/?day=7
    :param request: day-> 7 15 30
    :return: json_data
    """
    day = request.GET.get("day")
    if day == "7":
        date_day = datetime.now() - timedelta(days=7)
    elif day == "15":
        date_day = datetime.now() - timedelta(days=15)
    elif day == "30":
        date_day = datetime.now() - timedelta(days=30)
    else:
        date_day = datetime.now() - timedelta(days=7)
    v0 = len(Member.objects.filter(registerTime__gte=date_day))
    # 7天内新增会员
    v1 = len(Member.objects.filter(Q(registerTime__gte=date_day) & Q(type=0)))
    # 7天内新增领卡会员
    v2 = len(Member.objects.filter(Q(registerTime__gte=date_day) & Q(type=1)))
    # 7天内新增消费会员
    v3 = len(Member.objects.filter(Q(registerTime__gte=date_day) & Q(type=2)))
    data = {"v0": v0, "v1": v1, "v2": v2, "v3": v3}
    return JsonResponse({"data": data})